package com.IoLiu;

import java.io.*;
import java.util.ArrayList;
import java.util.Random;

public class CharacterIO {
    public static void main(String[] args) throws IOException {
        //characterIo();
        //characterJava();
        //characterLine();
        //copyDuYou();
        //ioArraysList();
        //arraysListio();
        dianMing();

    }

    private static void dianMing() throws IOException {
        Random ran=new Random();
        BufferedReader br=new BufferedReader(new FileReader("D:\\下载\\file\\file1\\student.txt"));
        ArrayList<String> list=new ArrayList<String>();
        String line;
        while((line=br.readLine())!=null){
            list.add(line);
        }
        br.close();
        System.out.println("开始随机点名了:");
        int rm=ran.nextInt(list.size());
        String dm=list.get(rm);
        System.out.println(dm+"同学请起来回答问题:");



    }

    private static void arraysListio() throws IOException {
        ArrayList<String> list=new ArrayList<String>();
        String[] ss={"张三","李四","王五","麻六","苏七","王八","李九","丽萍","小花","小草","校花","校草"};
        for (int i = 0; i < ss.length; i++) {
            list.add(ss[i]);
        }
        BufferedWriter bw=new BufferedWriter(new
                FileWriter("D:\\下载\\file\\file1\\test.txt",true));
        for (int i = 0; i < list.size(); i++) {
            bw.write(list.get(i));
            bw.newLine();
            bw.flush();
        }
        bw.close();
    }

    private static void ioArraysList() throws IOException {
        BufferedReader br=new BufferedReader(new
                FileReader("D:\\下载\\file\\file1\\字符流编码.txt"));
        ArrayList<String> list=new ArrayList<String>();
        String line;
        while((line=br.readLine())!=null){
            list.add(line);
        }

        br.close();


        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }

    private static void copyDuYou() throws IOException {
        BufferedWriter bw=new BufferedWriter(new
                FileWriter("D:\\下载\\file\\file1\\xie.java",true));
        BufferedReader br=new BufferedReader(new
                FileReader("D:\\下载\\file\\file1\\du.java"));
        String line;
        while((line=br.readLine())!=null){
            bw.write(line);
            bw.newLine();
            bw.flush();
        }
        bw.close();
        br.close();

    }

    private static void characterLine() throws IOException {
        BufferedWriter bw=new BufferedWriter(new
                FileWriter("D:\\下载\\file\\file1\\characteio.txt",true));
        BufferedReader br=new BufferedReader(new
                FileReader("D:\\下载\\file\\file1\\characteio.txt"));
//        for (int i = 0; i <3 ; i++) {
//            bw.write(" hello  "+i);
//            bw.newLine();
//            bw.flush();
//        }
        String line;
        while((line=br.readLine())!=null){
            System.out.println(line);
        }

    }

    private static void characterJava() throws IOException {
        BufferedReader br=new BufferedReader(new
                FileReader("D:\\下载\\file\\file1\\copy.java"));
        BufferedWriter bw=new BufferedWriter(new
                FileWriter("D:\\下载\\file\\file1\\songle.java",true));
        char[] ch=new char[1024];
        int len;
        while((len=br.read(ch))!=-1){
            bw.write(ch,0,len);
        }
    }

    private static void characterIo() throws IOException {
        System.out.println("写入输出开始了");
        BufferedReader br=new BufferedReader(new
                FileReader("D:\\下载\\file\\file1\\characteio.txt"));
        BufferedWriter bw=new BufferedWriter(new
                FileWriter("D:\\下载\\file\\file1\\characteio.txt",true));
        bw.write("Hello\r\n");
        bw.write("World\r\n");
        //不关闭bw,则本次写入读取不出来
        bw.flush();
        int len;
        char[] ch=new char[1024];
        while((len=br.read(ch))!=-1){
            System.out.println(new String(ch,0,len));
        }
        br.close();
        bw.close();
    }

}
